<!DOCTYPE html>
<html>
<head>
    <title>枣阳市第二人民医院管理系统--中软高科2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <style type="text/css">
        body {
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #f5f5f5;
        }

        .form-signin {
            max-width: 300px;
            padding: 19px 29px 29px;
            margin: 0 auto 20px;
            background-color: #fff;
            border: 1px solid #e5e5e5;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
            -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
            -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
            box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
        }

        .form-signin .form-signin-heading,
        .form-signin .checkbox {
            margin-bottom: 10px;
        }

        .form-signin input[type="text"],
        .form-signin input[type="password"] {
            font-size: 16px;
            height: auto;
            margin-bottom: 15px;
            padding: 7px 9px;
        }

    </style>
</head>
<body>
<div class="container">

    <form class="form-signin" id="myForm" method="post" action="/login">
        <h2 class="form-signin-heading">&nbsp;&nbsp;&nbsp;登录系统</h2>
        <input type="text" name="uLoginName" class="input-block-level" placeholder="账号" value="test">
        <input type="password" name="uPassWord" class="input-block-level" placeholder="密码" value="123">
        <input type="text" name="verify" class="input-medium" placeholder="验证码">
        <p id="verifyCode" style="display: inline;color:dodgerblue">----</p>

        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;<button id="b1" class="btn btn-large btn-primary" type="button">登录</button>
        </p>
    </form>

</div>

<script>
    // jquery 入口
    $(function () {
        loadVerify();

        // 加载验证码
        $("#verifyCode").click(function () {
            loadVerify();
        })

        // 登录认证
        $("#b1").click(function () {
            // 禁用表单登录功能
            event.preventDefault();
            // 获取表单元素
            const verify = $("input[name='verify']");
            const password = $("input[name='uPassWord']");
            const username = $("input[name='uLoginName']");
            // 表单空校验
            const flag = emptyCheck(username, password, verify);
            if (flag) {
                // 登录请求
                $.ajax({
                    type: "POST",
                    url: "/oauth/login",
                    dataType: "json",
                    data: $("#myForm").serialize(),
                    success: function (res) {
                        let code = res.code;
                        if (code === 200) {
                            alert(res.msg);
                            // 跳转至首页
                            $(location).attr("href", "/pages/index.html");
                        } else if (code === 500) {
                            alert(res.msg);
                        }
                    }
                });
            }
        });
    })

    // 加载验证码
    function loadVerify() {
        let $verifyCode = $("#verifyCode");
        // 获取后台生成验证码
        $.ajax({
            type: "GET",
            url: "/oauth/verify",
            success: function (res) {
                console.log(res);
                $verifyCode.html(res.obj);
                $("input[name='verify']").val(res.obj);
            },
            dataType: "json"
        });
    }

    // 登录字段空校验
    function emptyCheck(username, password, verify) {
        if (verify.val().length === 0) {
            alert("验证码不能为空");
            return false;
        } else if (password.val().length === 0 && username.val().length === 0) {
            alert("用户名和密码不能为空");
            return false;
        } else if (password.val().length === 0 || username.val().length === 0) {
            alert("用户名或密码不能为空");
            return false;
        }
        return true;
    }
</script>

</body>
</html>